package com.wrs.cems.index;

import com.wrs.cems.brower.BrowerOpenTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

public class LoginText {
    private static WebDriver edgeDriver;
    private WebDriver firefoxDriver;
    private WebDriver ieDriver;
    private WebDriver chromeDriver;

    public static void main(String[] args) throws InterruptedException {
        OpenURL();
        CheckLogin();
        comAccountTest();
        close();
    }

    //打开网址
    private static void OpenURL() {
        BrowerOpenTest.openEdge();
        edgeDriver = new EdgeDriver();
        edgeDriver.get("http://localhost:8080/cems");
    }

    //超级管理员账号登录测试
    @Test
    private static void CheckLogin() throws InterruptedException {

        //OpenURL();
        //窗口最大化
        //webDriver.manage().window().maximize();
        edgeDriver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
        WebElement account = edgeDriver.findElement(By.xpath("//input[@name=\"account\"]"));
        WebElement password = edgeDriver.findElement(By.name("password"));
        String act = account.getText();
        String pwd = password.getText();
       /* //显示等待
        WebDriverWait wait = new WebDriverWait(edgeDriver,3);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath()));*/
        if (!act.equals(null) || !act.equals("")) {
            account.clear();
            Thread.sleep(200);
            account.sendKeys("admin");
        }
        if (!pwd.equals(null) || !pwd.equals("")) {
            password.clear();
            Thread.sleep(200);
            password.sendKeys("admin123456");
        }
        edgeDriver.findElement(By.xpath("//input[@id=\"check1\"]")).click();
       /* Actions actions = new Actions(edgeDriver);
        actions.click();*/
        Thread.sleep(1000);
        edgeDriver.findElement(By.xpath("//button[text()=\"登录\"]")).click();
        Thread.sleep(1000);
        quitAccountTest();
    }

    //普通管理员账号登录测试
    @Test
    private static void comAccountTest() throws InterruptedException {
        //OpenURL();
        edgeDriver.get(edgeDriver.getCurrentUrl());
        edgeDriver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
        WebElement account = edgeDriver.findElement(By.xpath("//input[@name=\"account\"]"));
        WebElement password = edgeDriver.findElement(By.name("password"));
        String act = account.getText();
        String pwd = password.getText();
        if (!act.equals(null) || !act.equals("")) {
            account.clear();
            Thread.sleep(200);
            account.sendKeys("tet");
        }
        if (!pwd.equals(null) || !pwd.equals("")) {
            password.clear();
            Thread.sleep(200);
            password.sendKeys("tet123456");
        }
        edgeDriver.findElement(By.xpath("//input[@id=\"check2\"]")).click();

        Thread.sleep(1000);
        edgeDriver.findElement(By.xpath("//button[text()=\"登录\"]")).click();
        Thread.sleep(1000);
        quitAccountTest();
    }

    //退出账号测试
    @Test
    private static void quitAccountTest() throws InterruptedException {
       /* BrowerOpenTest.openEdge();
        edgeDriver = new EdgeDriver();
        edgeDriver.get("http://localhost:8080/cems/emps");*/
        edgeDriver.get(edgeDriver.getCurrentUrl());

        edgeDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        WebElement mouseMove = edgeDriver.findElement(By.xpath("//span[contains(@class,'2')]"));
        Actions actions = new Actions(edgeDriver);
        actions.moveToElement(mouseMove).perform();
        WebDriverWait wait = new WebDriverWait(edgeDriver, 3);
        WebElement quit = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("退出登录")));
        quit.click();
        Thread.sleep(1000);

    }

    @AfterClass
    private static void close() throws InterruptedException {
        Thread.sleep(3000);
        edgeDriver.close();
        edgeDriver.quit();
    }
}
